package com.haier.cloudremote;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.haier.cloudremote.GetStbLoadActivity.SetStbThread;
import com.haier.cloudremote.common.SLog;
import com.haier.cloudremote.model.StbInfo;
import com.haier.cloudremote.protocal.CAEProtocal;

/**
 * 当cae传过来多个机顶盒列表 用于选择cae传过来的城市机顶盒列表
 * 
 * @author sun
 * 
 */
public class SelectTownActivity extends Activity {
	private static final String TAG = "cloudremote";
	private ListView town_list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 设置全屏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.townselect);
		ActivityStackControlUtil.add(this);
		initView();
		town_list.setAdapter(new TownAdapter(GetStbLoadActivity.stbInfos));
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {
		// TODO Auto-generated method stub
		town_list = (ListView) findViewById(R.id.Town_ListView);
		// 用adpter填充
		SLog.d(TAG, "用adapter填充");
		town_list.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		town_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				// 将选中的城市列表存起来
				SetStbThread setStbThread = new SetStbThread(
						GetStbLoadActivity.stbInfos.get(arg2));
				setStbThread.start();
				SLog.d(TAG,
						"保存起来了，保存的是第"
								+ GetStbLoadActivity.stbInfos.get(arg2)
										.getStbName() + "项");
				Intent intent = new Intent();
				intent.setClass(SelectTownActivity.this, FinishActivity.class);
				startActivity(intent);
				finish();
			}

		});
	}

	private class TownAdapter extends BaseAdapter {
		private ArrayList<StbInfo> townresult;

		public TownAdapter(ArrayList<StbInfo> result) {
			this.townresult = result;
			SLog.d(TAG, "执行了------------TownAdapter-");
			// TODO Auto-generated constructor stub
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			SLog.d(TAG, "执行了------------getCount-" + townresult.size());
			return townresult.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			SLog.d(TAG, "执行了------------getItem-"
					+ townresult.get(arg0).getStbName());
			return townresult.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			SLog.d(TAG,
					"执行了------------getItemId，当前城市为==="
							+ townresult.get(arg0).getStbName());
			// TODO Auto-generated method stub
			return townresult.size();
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			SLog.d(TAG, "执行了------------getView-");
			if (arg1 == null) {
				arg1 = LayoutInflater.from(SelectTownActivity.this).inflate(
						R.layout.listitemtown, null);
				SLog.d(TAG, "view 不等于空");
			}
			TextView town = (TextView) arg1
					.findViewById(R.id.townlistitem_textView);
			if (townresult != null) {
				String str = townresult.get(arg0).getStbName();
				String a[] = str.split("-");
				str = a[1];
				for (int i = 0; i < a.length; i++) {
					if (!a[i].contains("学习")) {
						str = a[i];
						break;
					} else {
						str = a[1];
						break;
					}
				}
				// town.setText(townresult.get(arg0).getStbName());
				town.setText(str);
				SLog.d(TAG, "选择的城市=====" + str);
			} else {
				SLog.d(TAG, "townresult为空了");
			}
			return arg1;
		}
	}

	class SetStbThread extends Thread {
		StbInfo stbInfo;

		public SetStbThread(StbInfo stbInfo) {
			super();
			this.stbInfo = stbInfo;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			CAEProtocal.setStb(stbInfo);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			intent.setClass(SelectTownActivity.this, FinishActivity.class);
			startActivity(intent);
			finish();
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			intent.setClass(SelectTownActivity.this, TestListViewActivity.class);
			startActivity(intent);
			finish();
			break;
		case KeyEvent.KEYCODE_BACK:
			intent.setClass(SelectTownActivity.this, TestListViewActivity.class);
			startActivity(intent);
			finish();
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
}
